package metrics

import (
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/collectors"
	"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
	HTTPInFlightRequests = promauto.NewGauge(
		prometheus.GaugeOpts{
			Namespace: "netdisk",
			Name:      "http_inflight_requests",
			Help:      "Current number of in-flight HTTP requests.",
		},
	)

	HTTPRequestsTotal = promauto.NewCounterVec(
		prometheus.CounterOpts{
			Namespace: "netdisk",
			Name:      "http_requests_total",
			Help:      "Total number of HTTP requests processed.",
		},
		[]string{"method", "path", "status"},
	)

	HTTPRequestDurationSeconds = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: "netdisk",
			Name:      "http_request_duration_seconds",
			Help:      "Duration of HTTP requests in seconds.",
			Buckets:   prometheus.DefBuckets,
		},
		[]string{"method", "path"},
	)

	HTTPRequestSizeBytes = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: "netdisk",
			Name:      "http_request_size_bytes",
			Help:      "Size of HTTP requests in bytes.",
			// Buckets from 100B up to ~6MB
			Buckets: prometheus.ExponentialBuckets(100, 1.8, 10),
		},
		[]string{"method", "path"},
	)

	HTTPResponseSizeBytes = promauto.NewHistogramVec(
		prometheus.HistogramOpts{
			Namespace: "netdisk",
			Name:      "http_response_size_bytes",
			Help:      "Size of HTTP responses in bytes.",
			// Buckets from 100B up to ~6MB
			Buckets: prometheus.ExponentialBuckets(100, 1.8, 10),
		},
		[]string{"method", "path", "status"},
	)
)

func RegisterCollectors() {
	collectorsToRegister := []prometheus.Collector{
		collectors.NewGoCollector(),
		collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
		collectors.NewBuildInfoCollector(),
	}

	for _, collector := range collectorsToRegister {
		if err := prometheus.DefaultRegisterer.Register(collector); err != nil {
			if _, ok := err.(prometheus.AlreadyRegisteredError); ok {
				continue
			}
			panic(err)
		}
	}
}
